package com.nt.thah.service.impl;

import com.alibaba.fastjson.JSON;
import com.nt.thah.constant.SysCode;
import com.nt.thah.dao.SysConfigDao;
import com.nt.thah.entity.SysConfig;
import com.nt.thah.service.SysConfigService;
import com.nt.thah.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 系统配置Service
 * @author: ls
 * @Date: 2020-07-14 15:59:12
 */
@Slf4j
@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Resource
    private SysConfigDao sysConfigDao;

    @Resource
    private RedisUtil redisUtil;

    /**
     * @Description: 获取用户信息
     * @author: ls
     * @Date: 2020/7/21 20:34
     */
    @Override
    public List<SysConfig> getSysConfig() {
        String redisKey = SysCode.REDIS_KEY_SYS_CONFIG;
        String userStr = redisUtil.get(redisKey);
        if (StringUtils.isNotBlank(userStr)) {
            return JSON.parseArray(userStr, SysConfig.class);
        }
        List<SysConfig> configList = sysConfigDao.getSysConfigList();
        redisUtil.set(redisKey, JSON.toJSONString(configList));
        return configList;
    }

    /**
     * @Description: 获取系统配置列表
     * @Author: ls
     * @Date: 2020/09/03 18:45
     */
    @Override
    public List<SysConfig> getSysConfigList(String key) {
        List<SysConfig> configList = getSysConfig();
        return configList.stream().filter(obj -> obj.getKey().equals(key)).collect(Collectors.toList());
    }

    /**
     * @Description: 获取系统配置列表
     * @Author: ls
     * @Date: 2020/09/03 18:45
     */
    @Override
    public void updateConfig(String key, String value) {
        sysConfigDao.update(key, value);
    }

}